using System;
using System.Data;
using System.Data.SqlClient;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;

namespace EntLibEC.Dac.Basic
{
	/// <summary>
	/// Summary description for UserDac.
	/// </summary>
	public class UserDac
	{
		public UserDac()
		{
			//
			// TODO: Add constructor logic here
			//
		}

		

		public int Insert(UserInfo oParam)
		{
			string sql = @"INSERT INTO Ent_Sys_User
                            (
                            SysNo,UserID,UserName,
                            Pwd,Email,Phone,Note,
                            Status
                            )
                            VALUES (
                            @SysNo,@UserID,@UserName,
                            @Pwd,@Email,@Phone,@Note,
                            @Status
                            )";
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramUserID = new SqlParameter("@UserID", SqlDbType.NVarChar,10);
			SqlParameter paramUserName = new SqlParameter("@UserName", SqlDbType.NVarChar,20);
			SqlParameter paramPwd = new SqlParameter("@Pwd", SqlDbType.NVarChar,50);
			SqlParameter paramEmail = new SqlParameter("@Email", SqlDbType.NVarChar,50);
			SqlParameter paramPhone = new SqlParameter("@Phone", SqlDbType.NVarChar,100);
			SqlParameter paramNote = new SqlParameter("@Note", SqlDbType.NVarChar,200);
			SqlParameter paramStatus = new SqlParameter("@Status", SqlDbType.Int,4);

			paramSysNo.Value = oParam.SysNo;
			paramUserID.Value = oParam.UserID;
			paramUserName.Value = oParam.UserName;
			paramPwd.Value = oParam.Pwd;
			paramEmail.Value = oParam.Email;
			if ( oParam.Phone != AppConst.StringNull)
				paramPhone.Value = oParam.Phone;
			else
				paramPhone.Value = System.DBNull.Value;
			if ( oParam.Note != AppConst.StringNull )
				paramNote.Value = oParam.Note;
			else
				paramNote.Value = System.DBNull.Value;
			paramStatus.Value = oParam.Status;

			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramUserID);
			cmd.Parameters.Add(paramUserName);
			cmd.Parameters.Add(paramPwd);
			cmd.Parameters.Add(paramEmail);
			cmd.Parameters.Add(paramPhone);
			cmd.Parameters.Add(paramNote);
			cmd.Parameters.Add(paramStatus);

			return SqlHelper.ExecuteNonQuery(cmd);
		}

		public int Update(UserInfo oParam)
		{

			string sql = @"UPDATE Ent_Sys_User SET 
                            UserID=@UserID, 
                            UserName=@UserName, 
                            Pwd=@Pwd, Email=@Email, 
                            Phone=@Phone, Note=@Note,
                            Status=@Status
                            WHERE SysNo=@SysNo";
			SqlCommand cmd = new SqlCommand(sql);

			SqlParameter paramSysNo = new SqlParameter("@SysNo", SqlDbType.Int,4);
			SqlParameter paramUserID = new SqlParameter("@UserID", SqlDbType.NVarChar,10);
			SqlParameter paramUserName = new SqlParameter("@UserName", SqlDbType.NVarChar,20);
			SqlParameter paramPwd = new SqlParameter("@Pwd", SqlDbType.NVarChar,50);
			SqlParameter paramEmail = new SqlParameter("@Email", SqlDbType.NVarChar,50);
			SqlParameter paramPhone = new SqlParameter("@Phone", SqlDbType.NVarChar,100);
			SqlParameter paramNote = new SqlParameter("@Note", SqlDbType.NVarChar,200);
			SqlParameter paramStatus = new SqlParameter("@Status", SqlDbType.Int,4);

			paramSysNo.Value = oParam.SysNo;
			paramUserID.Value = oParam.UserID;
			paramUserName.Value = oParam.UserName;
			paramPwd.Value = oParam.Pwd;
			paramEmail.Value = oParam.Email;
			paramPhone.Value = oParam.Phone;
			paramNote.Value = oParam.Note;
			paramStatus.Value = oParam.Status;

			cmd.Parameters.Add(paramSysNo);
			cmd.Parameters.Add(paramUserID);
			cmd.Parameters.Add(paramUserName);
			cmd.Parameters.Add(paramPwd);
			cmd.Parameters.Add(paramEmail);
			cmd.Parameters.Add(paramPhone);
			cmd.Parameters.Add(paramNote);
			cmd.Parameters.Add(paramStatus);

			return SqlHelper.ExecuteNonQuery(cmd);
		}
	}
}
